﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Zyg.SSO.Core.Business
{
    public class AccountApp : BusinessBase<AccountApp, Guid>
    {
        public AccountApp()
        {
            // Users = new AccoutAppUserCollection(this, RelationType.OneToMany);
        }

        #region Fields and Properties

        private Guid accountID = Guid.Empty;
        private Guid appID = Guid.Empty;
        private DateTime expireTime;
        private Account account = null;
        private App app = null;

        /// <summary>
        /// Get or set the account identifier.
        /// </summary>
        public Guid AccountID
        {
            get { return accountID; }
            set
            {
                if (accountID != value)
                {
                    MarkChanged("AccountID");
                    accountID = value;
                }
            }
        }

        /// <summary>
        /// Get or set the app identifier.
        /// </summary>
        public Guid AppID
        {
            get { return appID; }
            set
            {
                if (appID != value)
                {
                    MarkChanged("AppID");
                    appID = value;
                }
            }
        }

        /// <summary>
        /// Get or set the expire time of app for account.
        /// </summary>
        public DateTime ExpireTime
        {
            get { return expireTime; }
            set
            {
                if (expireTime != value)
                {
                    MarkChanged("ExpireTime");
                    expireTime = value;
                }
            }
        }

        /// <summary>
        /// Get the specificated account.
        /// </summary>
        public Account Account
        {
            get
            {
                if (account == null || account.Id != accountID)
                    LoadAccount();

                return account;
            }
        }

        /// <summary>
        /// Get the specificated app.
        /// </summary>
        public App App
        {
            get
            {
                if (app == null || app.Id != appID)
                    LoadApp();

                return app;
            }
        }

        private AccoutAppUserCollection users = null;
        /// <summary>
        /// Get the relationed users.
        /// </summary>
        public AccoutAppUserCollection Users
        {
            get
            {
                if (users == null)
                    users = new AccoutAppUserCollection(this, RelationType.OneToMany);
                return users;
            }
            set
            {
                users = value;
                MarkChanged("Users");
            }
        }

        #endregion

        #region Base Overrides

        protected override void ValidationRules()
        {
            AddRule("Id", "标识不合法", Guid.Empty == Id);
        }

        protected override AccountApp DataSelect(Guid id)
        {
            return Services.DALService.SelectAccountApp(id);
        }

        protected override void DataUpdate()
        {
            Services.DALService.UpdateAccountApp(this);
        }

        protected override void DataInsert()
        {
            Services.DALService.InsertAccountApp(this);
        }

        protected override void DataDelete()
        {
            Services.DALService.DeleteAccountAppUsers(this);
            Services.DALService.DeleteAccountApp(this);
        }

        public override SaveAction Save()
        {
            if (this.IsDeleted && !this.IsNew)
            {
                this.Users.ClearOnSave();
                this.Users.Save();
            }

            SaveAction action = base.Save();

            if (!base.IsDeleted && users != null)
                Users.Save();

            return action;
        }

        #endregion

        #region Public Methods

        public static AccountApp Load(Guid accountId, Guid appId)
        {
            return Services.DALService.SelectAccountApp(accountId, appId);
        }

        #endregion

        #region Protected Methods

        private void LoadAccount()
        {
            account = Account.Load(this.AccountID);
        }

        private void LoadApp()
        {
            app = App.Load(this.AppID);
        }



        #endregion
    }
}
